package problems.contest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dubulingbo, 2023/7/23 10:38.
 */
public class WCT355 {
    /**
     * T1|6921. 按分隔符拆分字符串
     * <p>https://leetcode.cn/problems/split-strings-by-separator/</p>
     */
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> ret = new ArrayList<>();
        for (String w : words) handleSplit(ret, w, separator);
        return ret;
    }

    private void handleSplit(List<String> ans, String s, char separator) {
        int n = s.length();
        int pre = 0;
        for (int i = 0; i <= n; ++i) {
            if (i == n || s.charAt(i) == separator) {
                if (i - pre > 0)
                    ans.add(s.substring(pre, i));
                // 跳过当前分隔符
                pre = i + 1;
            }
        }
    }



    /**
     * T2|6915. 合并后数组中的最大元素
     * <p>https://leetcode.cn/problems/largest-element-in-an-array-after-merge-operations/</p>
     */
    public long maxArrayValue(int[] nums) {
        int n = nums.length;
        long suffixSum = nums[n - 1];
        long ret = nums[n - 1];

        for (int i = n - 2; i >= 0; --i) {
            if (nums[i] > suffixSum) {
                // 从新统计从当前位置开始的后缀和
                suffixSum = nums[i];
            } else {
                // 当前数小于已有的后缀和，则直接累加到已有的后缀和
                suffixSum += nums[i];
            }

            ret = Math.max(suffixSum, ret);
        }
        return ret;
    }

}
